package com.czp.menu.service.impl;

import org.apache.ibatis.session.RowBounds;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.czp.framework.model.DataStoreTemplate;
import com.czp.framework.util.DateUtils;
import com.czp.menu.mapper.IMenuMapper;
import com.czp.menu.model.Icon;
import com.czp.menu.model.Menu;
import com.czp.menu.service.IMenuService;
import com.czp.menu.util.MenuUtil;
import com.google.gson.Gson;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏
 * @version  version 1.0
 * @since    2014-6-9
 */
public class MenuServiceImpl implements IMenuService {
	private IMenuMapper menuMapper;
	private Gson gson = new Gson();
	
	public String getMenuList(String personId) throws Exception {
		return gson.toJson(
				MenuUtil.getChildrenByPid("0", menuMapper.getMenuList(personId)));
	}
	
	public String getSettingsListByPid(String pid) throws Exception {
		if(pid == null || pid.equals("")) {
			pid = "0";
		}
		return gson.toJson(
				menuMapper.getSettingsListByPid(pid));
	}
	
	public String getMenuTreeByPid(Menu menu) throws Exception {
		if(menu.getPid() == null || menu.getPid().equals("")) {
			menu.setPid("0");
		}
		return gson.toJson(menuMapper.getMenuTreeByPid(menu));
	}
	
	public int menuOperator(Menu menu) throws Exception {
		menu.setModifyDate(DateUtils.getSysTimestamp());
		
		if(menu.getCudType().equals("C")) {//添加
			menu.setCreateDate(DateUtils.getSysTimestamp());
			if(menu.getPid() != null && !menu.getPid().equals("")) {
				menu.setRoot("0");//非根节点
				menu.setMenuOrd(menuMapper.getMenuMaxOrderByPid(menu));
				if(menuMapper.addMenuNode(menu) > 0) {//当添加成功后
					return menuMapper.updateParentLeaf(menu.getPid());
				}
			} else {
				menu.setRoot("1");//根节点
				menu.setPid("0");//根节点标识
				menu.setMenuOrd(menuMapper.getMenuMaxOrderByPid(menu));
				return menuMapper.addMenuNode(menu);
			}
		} else if(menu.getCudType().equals("U")) {//修改
			return menuMapper.modifyMenuNode(menu);
		} else if(menu.getCudType().equals("D")) {//删除
			int count = menuMapper.deleteMenuNode(menu);
			menuMapper.updateParentLeaf(menu.getPid());
			return count;
		}
		return 0;
	}
	
	public int sortMenuNode(String jsonData, String pid) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		Menu menu = new Menu();
		menu.setPid(pid);
		for(int i = 0; i < jsonArr.size(); i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			menu.setId(jsonObj.getString("id"));
			menu.setMenuOrd(String.valueOf(i+1));//从1开始
			menuMapper.sortMenuNode(menu);
		}
		if(jsonArr.size() <= 1) {//当<=1时说明该父节点之前并没有子节点
			menuMapper.updateParentLeaf(pid);
		}
		return jsonArr.size();
	}
	
	public int updateParentLeaf(String pid) throws Exception {
		return menuMapper.updateParentLeaf(pid);
	}
	
	public String queryMenuIcon(Icon icon) throws Exception {
		RowBounds bounds = new RowBounds(icon.getStart(), icon.getLimit());
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(menuMapper.queryMenuIcon(icon, bounds));
		dst.setTotal(menuMapper.queryIconCount());
		return gson.toJson(dst);
	}
	
	public String getMenuRootList() throws Exception {
		return gson.toJson(menuMapper.getMenuRootList());
	}
	
	public String getMenuLeafByPid(String pid) throws Exception {
		return gson.toJson(menuMapper.getMenuLeafByPid(pid));
	}
	
	/**
	 * 获取 menuMapper
	 * @return 返回 menuMapper
	 */
	public IMenuMapper getMenuMapper() {
		return menuMapper;
	}
	/**
	 * 设置 menuMapper
	 * @param 对menuMapper进行赋值
	 */
	public void setMenuMapper(IMenuMapper menuMapper) {
		this.menuMapper = menuMapper;
	}
	
}
